package hkbigdata.test.app;


import hkbigdata.test.bean.Book;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * @author liuanbo
 * @creat 2024-02-27-7:13
 * @see 2194550857@qq.com
 */
public class test {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();

        String path="E:\\bigdata-teach\\2024-source\\javaweb\\code\\javaweb-server\\01_xml\\src\\hkbigdata\\test\\bookstore.xml";
        File file = new File(path);

        Document read = saxReader.read(file);
        System.out.println(read);

        Element rootElement = read.getRootElement();

        List<Element> books = rootElement.elements("book");

        Book book1 = new Book();
        for (Element book:books){
//            System.out.println(book.asXML());
            String id = book.attributeValue("id");
            String name = book.element("name").getText();
            String price = book.element("price").getText();
            String author = book.element("author").getText();

            book1.setId(id);
            book1.setName(name);
            book1.setPrice(Double.parseDouble(price));
            book1.setAuthor(author);

            System.out.println(book1);


        }
    }
}
